// pages/login/login.js
Page({
  data: {
    username: '',
    password: '',
    role: '学生'
  },
  // 角色选择处理
  handleRoleSelect(e) {
    const role = e.currentTarget.dataset.role;
    this.setData({ role });
  },

  onUsernameInput(e) {
    this.setData({ username: e.detail.value });
  },

  onPasswordInput(e) {
    this.setData({ password: e.detail.value });
  },

  handleLogin() {
    const { username, password,role } = this.data;
    
    if (!username || !password) {
      wx.showToast({
        title: '用户名和密码不能为空',
        icon: 'none'
      });
      return;
    }

    wx.request({
      url: 'http://192.168.172.134:5000/user/login',
      method: 'GET',
      data: { username, password,role },
      success: (res) => {
        if (res.statusCode === 200 && res.data.code===200) {
          wx.setStorageSync('token', res.data.token);
          wx.setStorageSync('username', username);
          wx.setStorageSync('role', role);
          wx.switchTab({
            url: '/pages/index/index'
          });
        } else {
          wx.showToast({
            title: res.data.message || '登录失败',
            icon: 'none'
          });
        }
      },
      fail: () => {
        wx.showToast({
          title: '网络错误',
          icon: 'none'
        });
      }
    });
  }
});